package pers.xiaojun.boot.module.system.service.permission;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import pers.xiaojun.boot.common.util.CollectionUtils;
import pers.xiaojun.boot.module.system.constants.RedisKeyConstants;
import pers.xiaojun.boot.module.system.dao.mapper.permission.RoleMenuMapper;
import pers.xiaojun.boot.module.system.dao.entity.permission.RoleMenuDO;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
 * 角色菜单实现类
 *
 * @author xiaojun
 * @since 2025-10-10
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenuDO> implements RoleMenuService {


    @Override
    @CacheEvict(value = RedisKeyConstants.Cache.ROLE_MENU_IDS, key = "#roleId")
    public void saveRoleMenus(Long roleId, Collection<Long> menuIds) {
        saveBatch(CollectionUtils.convertList(menuIds, menuId ->
                new RoleMenuDO().setRoleId(roleId).setMenuId(menuId)
        ));
    }

    @Override
    @CacheEvict(value = RedisKeyConstants.Cache.ROLE_MENU_IDS, allEntries = true) // 删除后清空角色拥有的菜单缓存
    public void removeByMenuId(Long menuId) {
        this.baseMapper.delete(new LambdaQueryWrapper<RoleMenuDO>()
                .eq(RoleMenuDO::getMenuId, menuId)
        );
    }

    @Override
    @CacheEvict(value = RedisKeyConstants.Cache.ROLE_MENU_IDS, allEntries = true) // 删除后清空角色拥有的菜单缓存
    public void removeByMenuIds(Collection<Long> menuIds) {
        this.baseMapper.delete(new LambdaQueryWrapper<RoleMenuDO>()
                .in(RoleMenuDO::getMenuId, menuIds)
        );
    }

    @Override
    @CacheEvict(value = RedisKeyConstants.Cache.ROLE_MENU_IDS, key = "#roleId")
    public void removeByRoleIdAndMenuIds(Long roleId, Collection<Long> menuIds) {
        this.baseMapper.delete(new LambdaQueryWrapper<RoleMenuDO>()
                .eq(RoleMenuDO::getRoleId, roleId)
                .in(RoleMenuDO::getMenuId, menuIds)
        );
    }

    @Override
    @CacheEvict(value = RedisKeyConstants.Cache.ROLE_MENU_IDS, key = "#roleId")
    public void removeByRoleId(Long roleId) {
        this.baseMapper.delete(new LambdaQueryWrapper<RoleMenuDO>()
                .eq(RoleMenuDO::getRoleId, roleId)
        );
    }

    @Override
    @CacheEvict(value = RedisKeyConstants.Cache.ROLE_MENU_IDS, key = "#roleIds")
    public void removeByRoleIds(List<Long> roleIds) {
        this.baseMapper.delete(new LambdaQueryWrapper<RoleMenuDO>()
                .in(RoleMenuDO::getRoleId, roleIds)
        );
    }

    @Override
    @Cacheable(value = RedisKeyConstants.Cache.ROLE_MENU_IDS, key = "#roleId")
    public List<Long> listMenuIdsByRoleId(Long roleId) {
        return this.baseMapper.selectObjs(new LambdaQueryWrapper<RoleMenuDO>()
                .select(RoleMenuDO::getMenuId)
                .eq(RoleMenuDO::getRoleId, roleId)
        );
    }

    @Override
    public List<Long> listMenuIdsByRoleIds(Collection<Long> roleIds) {
        HashSet<Long> menuIds = new HashSet<>();
        roleIds.forEach(roleId -> menuIds.addAll(self().listMenuIdsByRoleId(roleId)));
        return Lists.newArrayList(menuIds);
    }

    /**
     * 获取IoC容器中的自己
     */
    private RoleMenuService self() {
        return SpringUtil.getBean(RoleMenuService.class);
    }
}
